package com.pig4cloud.pig.demo.Util;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.demo.feign.Util.JWTUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by caoxuel on 2020/11/9 14:23
 */
public final class TokenUtils {

    public static Long getId(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token=null;
        if(cookies==null){
            token = request.getHeader("token");
        }else {
            for (int i = 0; i < cookies.length; i++) {
                if("token".equals(cookies[i].getName())){
                    token = cookies[i].getValue();
                }

            }
        }

        DecodedJWT verify = JWTUtil.verify(token);
        if (verify == null){
            return null;
        }
        long id = Long.parseLong(verify.getClaim("id").asString());
        return id;
    }
}
